package com.ophiux.lock.redis.redisLock;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.TimeUnit;

/**
 * @desc: 基于Redisson实现分布式锁
 * @author: hhl
 * @date：2021/11/3 16:40
 */
public class RedissonLockImpl implements RedisLock {

    private static final String LOCK_KEY = "hhl";

    @Autowired
    private Redisson redisson;

    @Override
    public boolean tryLock() throws Exception {

        //业务中直接执行这3行代码
        RLock redissonlock = redisson.getLock(LOCK_KEY);
        //只有第一个到达的才能获取锁
        redissonlock.lock(30, TimeUnit.SECONDS);

        //释放锁
        redissonlock.unlock();

        return false;
    }

    @Override
    public boolean releaseLock() {
        return false;
    }
}
